<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="品名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入品名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="品名" align="center" prop="name" />
      <el-table-column label="主要成分" align="center" prop="mainThing" />
      <el-table-column label="数量" align="center" prop="count" >
        <template slot-scope="scope">
          <span>{{ scope.row.count }}{{ scope.row.unit1Id }}/{{ scope.row.unit2Id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="规格" align="center" prop="ggCount" />
      <el-table-column label="保质到期" align="center" prop="endDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入库时间" align="center" prop="addDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="单位1" align="center" prop="unit1Id" />-->
<!--      <el-table-column label="单位2" align="center" prop="unit2Id" />-->
      <el-table-column label="休药期(天)" align="center" prop="anQq" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['busi:goodsInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['busi:goodsInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改投入品管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="品名" prop="name">
              <el-input v-model="form.name" placeholder="请输入品名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主要成分" prop="mainThing">
              <el-input v-model="form.mainThing" placeholder="请输入主要成分" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产企业" prop="factory">
              <el-input v-model="form.factory" placeholder="请输入生产企业" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="销售企业" prop="xiaoSqy">
              <el-input v-model="form.xiaoSqy" placeholder="请输入销售企业" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产批号" prop="shengCp">
              <el-input v-model="form.shengCp" placeholder="请输入生产批号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="批准文号" prop="piZwh">
              <el-input v-model="form.piZwh" placeholder="请输入批准文号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产许可证号" prop="hgzHao">
              <el-input v-model="form.hgzHao" placeholder="请输入生产许可证号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="规格" prop="ggCount">
              <el-input v-model="form.ggCount" placeholder="请输入规格" style="width: 120px;" />
              <el-select v-model="form.unit1Id" placeholder="请选择单位" style="width: 75px;">
                <el-option
                  v-for="dict in unitOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
              <el-select v-model="form.unit2Id" placeholder="请选择单位" style="width: 75px;">
                <el-option
                  v-for="dict in unitOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数量" prop="count">
              <el-input v-model="form.count" placeholder="请输入数量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产日期" prop="startDate">
              <el-date-picker clearable size="small"
                              v-model="form.startDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择生产日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="保质到期" prop="endDate">
              <el-date-picker clearable size="small"
                              v-model="form.endDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择保质到期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入库时间" prop="addDate">
              <el-date-picker clearable size="small"
                              v-model="form.addDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择入库时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="责任人" prop="staffId">
              <el-select v-model="form.staffId" placeholder="请选择养殖基地" >
                <el-option
                  v-for="companyStaff in companyStaffList"
                  :key="companyStaff.id"
                  :label="companyStaff.name"
                  :value="companyStaff.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="适用品种/阶段" prop="stage">
              <el-input v-model="form.stage" placeholder="请输入适用品种/阶段" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="休药期" prop="anQq">
              <el-input v-model="form.anQq" placeholder="请输入休药期" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="图片" prop="rs">
              <uploadImage v-model="form.rs"/>
            </el-form-item>
          </el-col>
        </el-row>


        <!--<el-form-item label="剂型" prop="jixingId">
          <el-input v-model="form.jixingId" placeholder="请输入剂型" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGoodsInfo, getGoodsInfo, delGoodsInfo, addGoodsInfo, updateGoodsInfo, exportGoodsInfo } from "@/api/busi/goodsInfo";
import { listCompanyStaff } from "@/api/busi/companyStaff";
import { getUserProfile } from "@/api/system/user";
import { getInfoByUser } from "@/api/busi/mainInfo";
import UploadImage from '@/components/UploadImage';

export default {
  name: "GoodsInfo",
  components: {
    UploadImage
  },
  data() {
    return {
      user:null,
      unitOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 投入品管理表格数据
      goodsInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mainId: null,
        name: null,
        type:'4',
      },
      companyStaffList:[],
      queryParamsCompanyStaff: {
        pageNum: 1,
        pageSize: 1000,
        mainId: null,
        name: null,
        staTypeId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        mainId: [
          { required: true, message: "主体id不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "品名不能为空", trigger: "blur" }
        ],
        mainThing: [
          { required: true, message: "主要成分不能为空", trigger: "blur" }
        ],
        shengCp: [
          { required: true, message: "生产批号不能为空", trigger: "blur" }
        ],
        ggCount: [
          { required: true, message: "规格不能为空", trigger: "blur" }
        ],
        count: [
          { required: true, message: "数量不能为空", trigger: "blur" }
        ],
        startDate: [
          { required: true, message: "生产日期不能为空", trigger: "blur" }
        ],
        endDate: [
          { required: true, message: "保质到期不能为空", trigger: "blur" }
        ],
        addDate: [
          { required: true, message: "入库时间不能为空", trigger: "blur" }
        ],
        staffId: [
          { required: true, message: "责任人不能为空", trigger: "blur" }
        ],
        anQq: [
          { required: true, message: "休药期不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getDicts("busi_unit").then(response => {
      this.unitOptions = response.data;
    });
    getUserProfile().then(response => {
      this.user = response.data;
      getInfoByUser({userId:this.user.userId}).then(response => {
        this.queryParams.mainId = response.data.id;
        this.queryParamsCompanyStaff.mainId = response.data.id;
        this.getList();
        this.getCompanyStaffs();
      });
    });
  },
  methods: {
    getCompanyStaffs() {
      listCompanyStaff(this.queryParamsCompanyStaff).then(response => {
        this.companyStaffList = response.rows;
      });
    },
    /** 查询投入品管理列表 */
    getList() {
      this.loading = true;
      listGoodsInfo(this.queryParams).then(response => {
        this.goodsInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        mainId: this.queryParams.mainId,
        name: null,
        mainThing: null,
        factory: null,
        xiaoSqy: null,
        shengCp: null,
        piZwh: null,
        hgzHao: null,
        ggCount: null,
        unit1Id: '亩',
        unit2Id: '亩',
        count: null,
        startDate: null,
        endDate: null,
        addDate: null,
        staffId: null,
        stage: null,
        rs: null,
        type: '4',
        anQq: null,
        jixingId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加投入品管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGoodsInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改投入品管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGoodsInfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoodsInfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除投入品管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delGoodsInfo(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有投入品管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportGoodsInfo(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
